package com.ruoyi.web.controller.parent;

import java.util.Date;
import java.util.List;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.BookVo;
import com.ruoyi.system.domain.vo.SlicesDetailVo;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Book;
import com.ruoyi.system.service.IBookService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预约Controller
 *
 * @author WuZhongfu
 * @date 2022-02-17
 */
@RestController
@RequestMapping("/parent/book")
public class BookController extends BaseController {
    @Autowired
    private IBookService bookService;

    /**
     * 查询预约列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Book book) {
        startPage();
        List<Book> list = bookService.selectBookList(book);
        return getDataTable(list);
    }

    /**
     * 查询预约列表(状态不为已取消)
     */
    @GetMapping("/selectBookNoCancel")
    public TableDataInfo selectBookNoCancel(Book book) {
        startPage();
        List<Book> list = bookService.selectBookNoCancel(book);
        return getDataTable(list);
    }

    @GetMapping("/listByDoctorId")
    public TableDataInfo listByDoctorId(Book book) {
        startPage();
        book.setUserId(SecurityUtils.getUserId());
        List<Book> list = bookService.selectBookList(book);
        return getDataTable(list);
    }

    /**
     * 导出预约列表
     */
    @PreAuthorize("@ss.hasPermi('parent:book:export')")
    @Log(title = "预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Book book) {
        List<Book> list = bookService.selectBookList(book);
        ExcelUtil<Book> util = new ExcelUtil<Book>(Book.class);
        util.exportExcel(response, list, "预约数据");
    }

    /**
     * 获取预约详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bookService.selectBookById(id));
    }

    /**
     * 新增预约
     */
    @PostMapping
    public AjaxResult add(@RequestBody BookVo bookVo) {
        try {
            return toAjax(bookService.insertBook(bookVo));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/checkFull")
    public AjaxResult checkFull(@RequestBody SlicesDetailVo slice) {
        try {
            return AjaxResult.success(bookService.checkFull(slice));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改预约
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Book book)  {
        int res = 0;
        try {
            res = bookService.updateBook(book);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return toAjax(res);
    }

    /**
     * 删除预约
     */
    @PreAuthorize("@ss.hasPermi('parent:book:remove')")
    @Log(title = "预约", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bookService.deleteBookByIds(ids));
    }
}
